/*  Copyright (c) 2011, Philip Busch <vzxwco@gmail.com>
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */


#ifndef NC_H
#define NC_H

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "list.h"
#include "stream.h"

#define NC_CLIENT 0
#define NC_SERVER 1

#define NC_BACKLOG 20

#define NC_AF_INET4 AF_INET
#define NC_AF_INET6 AF_INET6
#define NC_AF_AUTO  AF_UNSPEC

typedef struct nc nc;

typedef int(*nc_recognizer)(nc *);
typedef int(*nc_handler)(nc *, size_t len);

// main functions
nc     *nc_create(int);
int     nc_register_callback(nc *n, nc_recognizer r, nc_handler h);
int     nc_dial(nc *n, char *addr, char *port, short family, int nsec);
nc     *nc_talk();
void    nc_hangup(nc *n);

// use send() and recv() in your handler functions
int     nc_send(nc *n, char *buf, size_t len);
int     nc_recv(nc *n, char *buf, size_t len);
int     nc_peak(nc *n, char *buf, size_t len);
size_t  nc_size(nc *n);

// convenience recognizers
int     nc_is_connect(nc *n);
int     nc_is_disconnect(nc *n);
int     nc_is_error(nc *n);

// debugging
char   *nc_strerror(nc *n);
void    nc_print(nc *n);
void    nc_print_all(void);

#endif  /* ! NC_H */
